package com.longteng.testng.binder;

import com.longteng.testng.MethodParam;
import com.longteng.testng.annotation.Binding;
import com.longteng.testng.param.Pair;
import jodd.typeconverter.TypeConverterManager;

import java.lang.reflect.Parameter;
import java.util.List;

/**
 * Created by sky on 15-12-24
 * 主要用来处理来自java基本类型的注入
 */
@Binding
public class BaseBindPlugin extends BindPlugin {
    @Override
    public Object transfer(List<Pair> pairs, MethodParam parameter){
        if(pairs==null||pairs.size()!=1){
            throw new RuntimeException("参数名称和参数化文件匹配项个数不一致");
        }
        try{
            String value = pairs.get(0).getValue();
            return TypeConverterManager.convertType(value,parameter.getType());
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean filter(MethodParam parameter) {
        if(TypeConverterManager.lookup(parameter.getType())!=null){
            return true;
        }
        return false;
    }
}
